from data_processing import *
from utils.data_load import dataload
# 导入评估指标
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score
from xgboost import XGBClassifier

# 数据加载
X_train, X_test, y_train, y_test = dataload()

# 在训练集上进行特征转换
# 1. 特征编码和处理偏态分布
X_train, encoder, skewed_features = fit_transform_features(X_train)

# 2. 标准化数值特征
X_train, scaler, numeric_cols = fit_scale_features(X_train)

# 3. 处理类别不平衡（SMOTE）
X_train_balanced, y_train_balanced = handle_imbalance(X_train, y_train)

# 4. 特征选择
X_train_selected, selected_features, rfecv = select_features_cv(X_train_balanced, y_train_balanced)

# 在测试集上应用相同的转换
# 1. 特征编码和处理偏态分布
X_test = transform_features(X_test, encoder, skewed_features)

# 2. 标准化数值特征
X_test = scale_features(X_test, scaler, numeric_cols)

# 3. 特征选择
X_test_selected = X_test[selected_features]


# 初始化逻辑回归模型
model = LogisticRegression(max_iter=100, random_state=20)

# 在训练集上训练模型
model.fit(X_train_selected, y_train_balanced)

# 使用训练好的模型在测试集上进行预测
y_pred = model.predict(X_test_selected)

# 打印分类报告
print(classification_report(y_test, y_pred))

# 计算混淆矩阵
cm = confusion_matrix(y_test, y_pred)
print("混淆矩阵：\n", cm)

# 如果需要，计算 ROC AUC 分数
y_pred_proba = model.predict_proba(X_test_selected)[:, 1]
roc_auc = roc_auc_score(y_test, y_pred_proba)
print(f"ROC AUC Score: {roc_auc:.4f}")

# 初始化 XGBoost 模型
model = XGBClassifier(
    n_estimators=100,        # 树的数量
    learning_rate=0.09,       # 学习率
    max_depth=6,             # 树的最大深度
    random_state=20,         # 随机种子
    use_label_encoder=False, # 新版本中不需要 label encoder
    eval_metric='logloss'    # 评估指标
)

# 在训练集上训练模型
model.fit(X_train_selected, y_train_balanced)

# 使用训练好的模型在测试集上进行预测
y_pred = model.predict(X_test_selected)

# 打印分类报告
print(classification_report(y_test, y_pred))

# 计算混淆矩阵
cm = confusion_matrix(y_test, y_pred)
print("混淆矩阵：\n", cm)

# 计算 ROC AUC 分数
y_pred_proba = model.predict_proba(X_test_selected)[:, 1]
roc_auc = roc_auc_score(y_test, y_pred_proba)
print(f"ROC AUC Score: {roc_auc:.4f}")